package com.novel.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class InsertDao {

	public void insert(String section, String pageName, String content) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = Temple.getConnection();
			String sql = "insert into zanghaihua(sectionName,pageName,content) values(?,?,?)";

			ps = conn.prepareStatement(sql);
			ps.setString(1, section);
			ps.setString(2, pageName);
			ps.setString(3, content);

			ps.executeUpdate();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Temple.free(conn, ps, rs);
		}
	}

	public int getCount() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = Temple.getConnection();
			String sql = "select count(*) from zanghaihua";

			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Temple.free(conn, ps, rs);
		}
		return 0;
	}

	public Map<String, List<NovelBean>> getTitles() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<String> sectionName = new ArrayList<String>();
		Map<String, List<NovelBean>> map = new LinkedHashMap<String, List<NovelBean>>();
		
		try {
			conn = Temple.getConnection();
			String sql = "select distinct sectionName from zanghaihua";

			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				sectionName.add(rs.getString("sectionName"));
			}

			for (String name : sectionName) {
				sql = "select id,pageName from zanghaihua where sectionName = ?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, name);
				rs = ps.executeQuery();
				List<NovelBean> list = new ArrayList<NovelBean>();
				while (rs.next()) {
					NovelBean bean = new NovelBean();
					bean.setId(rs.getString("id"));
					bean.setPageName(rs.getString("pageName").trim());
					list.add(bean);
				}
			//	System.out.println("key=="+name+"==");
				map.put(name, list);
			}
			
		/*	for(Entry<String, List<NovelBean>> s:map.entrySet()){
				System.out.println(s.getKey());
				System.out.println(s.getValue().size());
			//	System.out.println(map.get(s.getKey()));
			
			}*/
			
		//	System.out.println(map.get("第一卷").size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Temple.free(conn, ps, rs);
		}
		return map;
	}
	
	public NovelBean getContent(String id){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = Temple.getConnection();
			String sql = "select pageName,content from zanghaihua where id = ?";

			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				NovelBean bean = new NovelBean();
				bean.setPageName(rs.getString("pageName"));
				bean.setContent(rs.getString("content"));
				
				return bean;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Temple.free(conn, ps, rs);
		}
		return null;
	}

	

	public static void main(String[] args) {
		InsertDao dao = new InsertDao();
		// dao.insert("�Ǻ�", "��һ��", "����");
		 System.out.println(dao.getCount());
		//dao.getTitles();
		//System.out.println(dao.getContent("12"));
	}
}
